package org.javaapps.demo.service.impl;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.javaapps.common.dao.IBaseDao;
import org.javaapps.common.pagination.Page;
import org.javaapps.common.pagination.PageUtil;
import org.javaapps.common.service.impl.BaseService;
import org.javaapps.demo.dao.UserDao;
import org.javaapps.demo.model.UserModel;
import org.javaapps.demo.model.UserQueryModel;
import org.javaapps.demo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * User: Lin Jianxiong
 * Date: 12-1-4 上午11:06
 * Version: 1.0
 */

@Service("UserService")
public class UserServiceImpl extends BaseService<UserModel, Integer> implements UserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);

    private UserDao userDao;

    @Autowired
    @Qualifier("UserDao")
    @Override
    public void setBaseDao(IBaseDao<UserModel, Integer> userDao) {
        this.baseDao = userDao;
        this.userDao = (UserDao) userDao;
    }
    
    public UserModel getUserByNamePass(UserModel user){
    	List<UserModel> userList = userDao.listHql("from UserModel where username = ? and password = ?", -1, -1, null, user.getUsername(), user.getPassword());
    	if(userList.size() == 0)
    		return null;
    	return userList.get(0);
    }

    @Override
    public Page<UserModel> query(int pn, int pageSize, UserQueryModel command) {
        return PageUtil.getPage(userDao.countQuery(command) ,pn, userDao.query(pn, pageSize, command), pageSize);
    }

    public @NotNull UserModel login(@NotNull String username, @NotNull String password){
    	UserQueryModel command = new UserQueryModel();
    	command.setUsername(username);
    	command.setPassword(password);
    	Page<UserModel> userPage = query(0, 1, command);
    	return userPage.getItems().get(0);
    }

	@Override
	public UserModel getUser(UserModel user) {
		return userDao.getUser(user);
	}
}